package com.xiaomai.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomai.eduservice.entity.EduCourse;
import com.xiaomai.eduservice.entity.EduCourseDescription;
import com.xiaomai.eduservice.entity.frontVo.CourseFrontVo;
import com.xiaomai.eduservice.entity.frontVo.CourseWebVo;
import com.xiaomai.eduservice.entity.vo.CourseInfoVo;
import com.xiaomai.eduservice.entity.vo.CoursePublishVo;
import com.xiaomai.eduservice.mapper.EduCourseMapper;
import com.xiaomai.eduservice.service.EduChapterService;
import com.xiaomai.eduservice.service.EduCourseDescriptionService;
import com.xiaomai.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomai.eduservice.service.EduVideoService;
import com.xiaomai.servicebase.config.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
     private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert==0){
            throw  new GuliException(20001,"添加失败");
        }
        //获取课程id
        String cid = eduCourse.getId();


        //向简介表中添加数据
        EduCourseDescription courseDescription = new EduCourseDescription();

        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        eduCourseDescriptionService.save(courseDescription);

        //将课程简介id绑定课程id

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //根据Id获取课程信息
        //1.获取信息并封装到course里面
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2.封装简介到courseinfo里面
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改基础信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i==0){
            throw new GuliException(20001,"修改数据失败");
        }

        //2.修改简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        //删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //删除描述
        eduCourseDescriptionService.removeById(courseId);
        //删除课程
        boolean flag = this.removeById(courseId);
        if (!flag){
            throw new GuliException(20001,"删除课程失败");
        }


    }

    //课程查询-分页条件
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> pageCourse, CourseFrontVo courseVo) {
        //创建查询的条件对象
        QueryWrapper coursewrapper = new QueryWrapper();
        //查询是否有点击一级分类
        if (!StringUtils.isEmpty(courseVo.getSubjectParentId())) {
            coursewrapper.eq("subject_parent_id", courseVo.getSubjectParentId());
        }
        //查询是否有点击二级分类
        if (!StringUtils.isEmpty(courseVo.getSubjectId())) {
            coursewrapper.eq("subject_id", courseVo.getSubjectId());
        }
        //查询是否有点击关注度分类
        if (!StringUtils.isEmpty(courseVo.getBuyCountSort())) {
            coursewrapper.orderByDesc("buy_count");
        }
        //最新
        if (!StringUtils.isEmpty(courseVo.getGmtCreateSort())) {
            coursewrapper.orderByDesc("gmt_create");
        }
        //价格
        if (!StringUtils.isEmpty(courseVo.getPriceSort())) {
            coursewrapper.orderByDesc("price");
        }

        //最终的查询方法，需要传入分页和条件
        baseMapper.selectPage(pageCourse,coursewrapper);

        //获得分页对象中的各数据
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();
        //创建map集合，并将获得到的数据封装进里面
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    //查询课程详情信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
       return baseMapper.getBaseCourseInfo(courseId);

    }


}
